package com.agame.spidercard;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.Entity;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.color.Color;

import android.util.Log;

import com.agame.spidercard.adt.card.Card;
import com.agame.spidercard.sprite.CardSprite;
import com.agame.spidercard.util.CardFactory;

/**
 * (c) 2010 Nicolas Gramlich (c) 2011 Zynga
 * 
 * @author Nicolas Gramlich
 * @since 18:20:40 - 18.06.2010
 */
public class SpiderGame extends SimpleBaseGameActivity {
	// ===========================================================
	// Constants
	// ===========================================================

	public static final int CAMERA_WIDTH = 800;
	public static final int CAMERA_HEIGHT = 480;

	private static final int TOP = 10;
	public static final int START_POSION = 50;
	public static final int SPACE = 50;
	public static final int TOP_SPACE =20 ;
	
	// ===========================================================
	// Fields
	// ===========================================================

	public static int[] cols = { START_POSION,
			START_POSION + (Card.CARD_WIDTH + SPACE) * 1,
			START_POSION + (Card.CARD_WIDTH + SPACE) * 2,
			START_POSION + (Card.CARD_WIDTH + SPACE) * 3,
			START_POSION + (Card.CARD_WIDTH + SPACE) * 4,
			START_POSION + (Card.CARD_WIDTH + SPACE) * 5 };

	private Camera mCamera;
	private BitmapTextureAtlas mCardDeckTexture;

	private Scene mScene;

	private HashMap<Card, ITextureRegion> mCardTotextureRegionMap;

	public static Map<Integer, CardList> cardlistMap = new HashMap<Integer, CardList>();

	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	@Override
	public EngineOptions onCreateEngineOptions() {
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

		final EngineOptions engineOptions = new EngineOptions(true,
				ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(
						CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
		engineOptions.getTouchOptions().setNeedsMultiTouch(true);
		return engineOptions;
	}

	public Scene getmScene() {
		return mScene;
	}

	@Override
	public void onCreateResources() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

		this.mCardDeckTexture = new BitmapTextureAtlas(
				this.getTextureManager(), 1024, 512, TextureOptions.BILINEAR);
		BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				this.mCardDeckTexture, this, "carddeck_tiled.png", 0, 0);
		this.mCardDeckTexture.load();

		this.mCardTotextureRegionMap = new HashMap<Card, ITextureRegion>();

		/* Extract the TextureRegion of each card in the whole deck. */
		for (final Card card : Card.values()) {
			final ITextureRegion cardTextureRegion = TextureRegionFactory
					.extractFromTexture(this.mCardDeckTexture,
							card.getTexturePositionX(),
							card.getTexturePositionY(), Card.CARD_WIDTH,
							Card.CARD_HEIGHT);
//			Log.v("CARD",card.getTexturePositionX()+"||"+card.getTexturePositionY()) ;
			this.mCardTotextureRegionMap.put(card, cardTextureRegion);
		}

	}

	@Override
	public Scene onCreateScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());
		this.mScene = new Scene();
		this.mScene.setOnAreaTouchTraversalFrontToBack();
		List<Card> list = CardFactory.getInstance().getSetListWithMix();
		for (int start : cols) {
			CardList cardlist = new CardList(start, 0);
			cardlistMap.put(start, cardlist);
		}
		int j=-1 ;
		for (int key : cardlistMap.keySet()) {
			j++;
			CardList cardlist = cardlistMap.get(key);
			Rectangle coloredRect = new Rectangle(cardlist.getX(), 0, Card.CARD_WIDTH, Card.CARD_HEIGHT, this.getVertexBufferObjectManager());
			coloredRect.setColor(Color.RED) ;
			this.mScene.attachChild(coloredRect);
			for (int i = 0; i < 6; i++) {
				this.addCard(list.get(j*6+i), cardlist);
			}
		}
		this.mScene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

		this.mScene.setTouchAreaBindingOnActionDownEnabled(true);
//		this.mScene.registerUpdateHandler(new TimerHandler(0.5f,true, new ITimerCallback() {
//			
//			@Override
//			public void onTimePassed(TimerHandler pTimerHandler) {
//				if()
//				resetPosition() ;
//			}
//		})) ;
		return this.mScene;
	}				

	// ===========================================================
	// Methods
	// ===========================================================

	private void addCard(final Card pCard, CardList entity) {
		final CardSprite sprite = new CardSprite(entity.getX(), entity.getY()
				+ entity.getCardSpriteList().size() * TOP_SPACE,
				this.mCardTotextureRegionMap.get(pCard),
				this.getVertexBufferObjectManager(), pCard,this);
		sprite.setParent(entity);
		entity.getCardSpriteList().add(sprite);
		this.mScene.attachChild(sprite);
		this.mScene.registerTouchArea(sprite);
	}

	public void updatePosition(){
		for(int key :cardlistMap.keySet()){
			CardList list = cardlistMap.get(key);
			List<CardSprite> card = list.getCardSpriteList() ;
			for(int i =0;i<card.size();i++){
				CardSprite sprite = card.get(i) ;
				sprite.setPosition(list.getX(), list.getY()+i*TOP_SPACE) ;
			}
		}
	}
	
	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
